package com.portal.cwegportal.util;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Date;
import java.util.Random;

@Service
public class Upload {

    /**
     * 处理文件上传方法封装(单张图片)
     * @param imageFile
     * @param request
     * @return
     * @throws Exception
     */
    public static String fileUpload(MultipartFile imageFile, HttpServletRequest request) throws Exception{
        String fileName = null;
        if(!imageFile.isEmpty()){
            String filePath="D:\\\\cwegImage";

            //重新生成文件名字
            fileName = new Date().getTime()+""+new Random().nextInt()+"."+imageFile.getOriginalFilename().split("\\.")[1];
            //将文件保存到指定目录
            imageFile.transferTo(new File(filePath+"/"+fileName));
        }
        //返回文件名字供保存
        return fileName;
    }
}
